/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TextTab.java
 *
 * Created on 1 mars 2010, 10:07:33
 */
package chat.client;

import chat.common.*;

import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.util.Calendar;
import java.text.SimpleDateFormat;

/**
 * The application TextTab for display tabs.
 * @author berthise
 */
public class TextTab extends javax.swing.JPanel {

    private String idTab = new String();
    private Communication communication;
    private ChatView client = null;
    private boolean select;

    /**
     * Creates new form TextTab, conetnant un text area, un zone de saisie, principalement
     * Creation de l'evenement de resize de la fenetre pour le splite
     * @param client le parent
     * @param communication la communication pour facilite les echanges
     * @param idTab l'identifiant du tab
     */
    public TextTab(ChatView client, Communication communication, String idTab) {
        this.client = client;
        this.idTab = idTab;
        this.communication = communication;
        this.select = true;

        initComponents();

        jSplitPane1.addComponentListener(new java.awt.event.ComponentAdapter() {

            @Override
            public void componentResized(ComponentEvent event) {
                jSplitPane1.setDividerLocation(jSplitPane1.getHeight() - 40); // quand la fentre resize
            }
        });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jSplitPane1 = new javax.swing.JSplitPane();
        textAreaPanel = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        CommandPanel = new javax.swing.JPanel();
        labelMessage = new javax.swing.JLabel();
        textMessage = new javax.swing.JTextField();
        sendButton = new javax.swing.JButton();

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("Form"); // NOI18N
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(225, 0));
        setLayout(new java.awt.BorderLayout());

        jSplitPane1.setDividerLocation(0);
        jSplitPane1.setDividerSize(5);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setAlignmentY(3.0F);
        jSplitPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jSplitPane1.setDoubleBuffered(true);
        jSplitPane1.setName("jSplitPane1"); // NOI18N
        jSplitPane1.setPreferredSize(new java.awt.Dimension(225, 0));

        textAreaPanel.setName("textAreaPanel"); // NOI18N
        textAreaPanel.setOpaque(false);
        textAreaPanel.setPreferredSize(new java.awt.Dimension(223, 0));

        textArea.setEditable(false);
        textArea.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        textArea.setName("textArea"); // NOI18N
        textAreaPanel.setViewportView(textArea);

        jSplitPane1.setTopComponent(textAreaPanel);

        CommandPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        CommandPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        CommandPanel.setMaximumSize(new java.awt.Dimension(32767, 0));
        CommandPanel.setMinimumSize(new java.awt.Dimension(73, 0));
        CommandPanel.setName("CommandPanel"); // NOI18N
        CommandPanel.setPreferredSize(new java.awt.Dimension(30, 10));
        CommandPanel.setLayout(new java.awt.BorderLayout(9, 9));

        labelMessage.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(TextTab.class);
        labelMessage.setText(resourceMap.getString("labelMessage.text")); // NOI18N
        labelMessage.setAlignmentX(0.5F);
        labelMessage.setBorder(javax.swing.BorderFactory.createEmptyBorder(3, 3, 3, 3));
        labelMessage.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        labelMessage.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        labelMessage.setMaximumSize(new java.awt.Dimension(73, 14));
        labelMessage.setMinimumSize(new java.awt.Dimension(73, 14));
        labelMessage.setName("labelMessage"); // NOI18N
        labelMessage.setPreferredSize(new java.awt.Dimension(73, 14));
        CommandPanel.add(labelMessage, java.awt.BorderLayout.WEST);

        textMessage.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        textMessage.setMargin(new java.awt.Insets(3, 3, 3, 3));
        textMessage.setName("textMessage"); // NOI18N
        textMessage.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textMessageKeyPressed(evt);
            }
        });
        CommandPanel.add(textMessage, java.awt.BorderLayout.CENTER);

        sendButton.setText(resourceMap.getString("sendButton.text")); // NOI18N
        sendButton.setAlignmentX(0.5F);
        sendButton.setDefaultCapable(false);
        sendButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        sendButton.setName("sendButton"); // NOI18N
        sendButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                sendButtonMouseClicked(evt);
            }
        });
        CommandPanel.add(sendButton, java.awt.BorderLayout.EAST);

        jSplitPane1.setBottomComponent(CommandPanel);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void sendButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sendButtonMouseClicked
        sendMessage();
    }//GEN-LAST:event_sendButtonMouseClicked

    private void textMessageKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textMessageKeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            sendMessage();
        }
}//GEN-LAST:event_textMessageKeyPressed

    /**
     * on recupere le message dans le textfield s'il est pas vide
     * puis l'envois a la connection
     */
    private void sendMessage() {
        // TODO add your handling code here:
        String text = new String();
        if (textMessage.getSelectedText() == null) {
            text = textMessage.getText();
            if (!text.equals("")) {
                textMessage.setText("");
                communication.sendMessage(text, this.getIdTab());
                writeMessage(text, "Me");
            }
        }
    }

    /**
     * Affiche le message dans le text area.
     * @param mess le contenu du message
     * @param src la source du message
     */
    public void writeMessage(String mess, String src) {

        /**
         * on affiche l'heure, puis le message
         */
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        textArea.append("<" + src + ">");
        textArea.append(dateNow + " : ");
        textArea.append(mess);
        textArea.append("\n");
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel CommandPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel labelMessage;
    private javax.swing.JButton sendButton;
    private javax.swing.JTextArea textArea;
    private javax.swing.JScrollPane textAreaPanel;
    private javax.swing.JTextField textMessage;
    // End of variables declaration//GEN-END:variables

    /**
     *
     * @param bool
     */
    public void setEnsable(Boolean bool) {
        sendButton.setEnabled(bool);
        textMessage.setEnabled(bool);
    }

    /*
     * getteur et setteur
     */
    /**
     *
     * @return
     */
    public String getIdTab() {
        return idTab;
    }

    /**
     *
     * @return
     */
    public Communication getConnection() {
        return communication;
    }

    /**
     *
     * @param communication
     */
    public void setConnection(Communication communication) {
        this.communication = communication;
    }

    /**
     *
     * @param idTab
     */
    public void setIdTab(String idTab) {
        this.idTab = idTab;
    }

    /**
     *
     * @return
     */
    public JPanel getCommandPanel() {
        return CommandPanel;
    }

    /**
     *
     * @return
     */
    public JSplitPane getjSplitPane1() {
        return jSplitPane1;
    }

    /**
     *
     * @return
     */
    public JTextArea getTextArea() {
        return textArea;
    }

    /**
     *
     */
    public void select() {
        this.select = true;
    }

    /**
     *
     */
    public void unSelect() {
        this.select = false;
    }

    /**
     *
     * @return
     */
    public boolean isSelect() {
        return select;
    }

    /**
     *
     * @param obj
     * @return
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final TextTab other = (TextTab) obj;
        if ((this.idTab == null) ? (other.idTab != null) : !this.idTab.equals(other.idTab)) {
            return false;
        }
        return true;
    }

    /**
     *
     * @return
     */
    @Override
    public int hashCode() {
        int hash = 5;
        hash = 37 * hash + (this.idTab != null ? this.idTab.hashCode() : 0);
        return hash;
    }
}

